# -*- Makefile -*- for GNU Emacs on the Microsoft Windows API.
# Copyright (C) 2011-2015 Free Software Foundation, Inc.

# This file is part of GNU Emacs.

# GNU Emacs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# GNU Emacs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

ALL = gnulib

.PHONY: $(ALL)

LOCAL_FLAGS	= -I. -I../nt/inc -I../src
LIBS 		=

GNULIBOBJS = $(BLD)/acl-errno-valid.$(O) \
	     $(BLD)/c-ctype.$(O) \
	     $(BLD)/c-strcasecmp.$(O) \
	     $(BLD)/c-strncasecmp.$(O) \
	     $(BLD)/close-stream.$(O) \
	     $(BLD)/dtoastr.$(O) \
	     $(BLD)/dtotimespec.$(O)  \
	     $(BLD)/execinfo.$(O)  \
	     $(BLD)/fpending.$(O)  \
	     $(BLD)/getopt.$(O)  \
	     $(BLD)/getopt1.$(O) \
	     $(BLD)/gettime.$(O) \
	     $(BLD)/memrchr.$(O) \
	     $(BLD)/strftime.$(O) \
	     $(BLD)/time_r.$(O) \
	     $(BLD)/timespec-add.$(O) \
	     $(BLD)/timespec-sub.$(O) \
	     $(BLD)/md5.$(O) \
	     $(BLD)/sha1.$(O) \
	     $(BLD)/sha256.$(O) \
	     $(BLD)/sha512.$(O) \
	     $(BLD)/sig2str.$(O) \
	     $(BLD)/stat-time.$(O) \
	     $(BLD)/timespec.$(O) \
	     $(BLD)/u64.$(O) \
	     $(BLD)/filemode.$(O)

#
# Build the library
#
$(BLD)/libgnu.$(A):	$(GNULIBOBJS)
	- $(DEL) $@
	$(AR) $(AR_OUT)$@ $(ALL_DEPS)

gnulib:		$(BLD)/libgnu.$(A)

#
# Build everything
#
all:		$(ALL)

### TAGS ###

FRC:

TAGS: FRC
	 ../lib-src/$(BLD)/etags.exe *.c *.h

### DEPENDENCIES ###

EMACS_ROOT	= ..
GNU_LIB		= .
SRC		= $(EMACS_ROOT)/src
NT_INC		= $(EMACS_ROOT)/nt/inc

ACL_H	       = $(GNU_LIB)/acl.h \
		 $(NT_INC)/sys/stat.h \
		 $(NT_INC)/stdbool.h
C_CTYPE_H      = $(GNU_LIB)/c-ctype.h \
		 $(NT_INC)/stdbool.h
MS_W32_H       = $(NT_INC)/ms-w32.h \
		 $(NT_INC)/sys/stat.h
CONF_POST_H    = $(SRC)/conf_post.h \
		 $(MS_W32_H)
CONFIG_H       = $(SRC)/config.h \
		 $(CONF_POST_H)
FILEMODE_H     = $(GNU_LIB)/filemode.h \
		 $(NT_INC)/sys/stat.h
FTOASTR_H      = $(GNU_LIB)/ftoastr.h \
		 $(GNU_LIB)/intprops.h
FTOASTR_C      = $(GNU_LIB)/ftoastr.c \
		 $(CONFIG_H) \
		 $(FTOASTR_H)
GETOPT_INT_H   = $(GNU_LIB)/getopt_int.h \
		 $(GNU_LIB)/getopt.h
MD5_H          = $(GNU_LIB)/md5.h \
		 $(NT_INC)/stdint.h
SHA1_H         = $(GNU_LIB)/sha1.h \
		 $(NT_INC)/stdint.h
SHA256_H       = $(GNU_LIB)/sha256.h \
		 $(NT_INC)/stdint.h
U64_H          = $(GNU_LIB)/u64.h \
		 $(NT_INC)/stdint.h
SHA512_H       = $(GNU_LIB)/sha512.h \
		 $(U64_H)
SIG2STR_H      = $(GNU_LIB)/sig2str.h \
		 $(GNU_LIB)/intprops.h
STAT_TIME_H    = $(GNU_LIB)/stat-time.h \
		 $(NT_INC)/sys/stat.h

$(BLD)/acl-errno-valid.$(O) : \
	$(GNU_LIB)/acl-errno-valid.c \
	$(ACL_H) \
	$(CONFIG_H)

$(BLD)/c-ctype.$(O) : \
	$(GNU_LIB)/c-ctype.c \
	$(CONFIG_H) \
	$(C_CTYPE_H)

$(BLD)/c-strcasecmp.$(O) : \
	$(GNU_LIB)/c-strcasecmp.c \
	$(GNU_LIB)/c-strcase.h \
	$(CONFIG_H) \
	$(C_CTYPE_H)

$(BLD)/c-strncasecmp.$(O) : \
	$(GNU_LIB)/c-strncasecmp.c \
	$(GNU_LIB)/c-strcase.h \
	$(CONFIG_H) \
	$(C_CTYPE_H)

$(BLD)/close-stream.$(O) : \
	$(GNU_LIB)/close-stream.c \
	$(GNU_LIB)/close-stream.h \
	$(GNU_LIB)/fpending.h \
	$(NT_INC)/stdbool.h \
	$(CONFIG_H)

$(BLD)/dtoastr.$(O) : \
	$(GNU_LIB)/dtoastr.c \
	$(FTOASTR_C)

$(BLD)/dtotimespec.$(O) : \
	$(GNU_LIB)/dtotimespec.c \
	$(GNU_LIB)/intprops.h \
	$(GNU_LIB)/timespec.h \
	$(CONFIG_H)

$(BLD)/execinfo.$(O) : \
	$(GNU_LIB)/execinfo.c \
	$(GNU_LIB)/execinfo.h \
	$(CONFIG_H)

$(BLD)/fpending.$(O) : \
	$(GNU_LIB)/fpending.c \
	$(GNU_LIB)/fpending.h \
	$(CONFIG_H)

$(BLD)/getopt.$(O) : \
	$(GNU_LIB)/getopt.c \
	$(GNU_LIB)/getopt.h \
	$(GNU_LIB)/gettext.h \
	$(NT_INC)/unistd.h \
	$(CONFIG_H) \
	$(GETOPT_INT_H)

$(BLD)/getopt1.$(O) : \
	$(GNU_LIB)/getopt1.c \
	$(GNU_LIB)/getopt.h \
	$(CONFIG_H) \
	$(GETOPT_INT_H)

$(BLD)/gettime.$(O) : \
	$(GNU_LIB)/gettime.c \
	$(GNU_LIB)/timespec.h \
	$(NT_INC)/sys/time.h \
	$(CONFIG_H)

$(BLD)/strftime.$(O) : \
	$(GNU_LIB)/strftime.c \
	$(GNU_LIB)/strftime.h \
	$(NT_INC)/stdbool.h \
	$(CONFIG_H)

$(BLD)/time_r.$(O) : \
	$(GNU_LIB)/time_r.c \
	$(CONFIG_H)

$(BLD)/timespec-add.$(O) : \
	$(GNU_LIB)/timespec-add.c \
	$(GNU_LIB)/intprops.h \
	$(GNU_LIB)/timespec.h \
	$(CONFIG_H)

$(BLD)/timespec-sub.$(O) : \
	$(GNU_LIB)/timespec-sub.c \
	$(GNU_LIB)/intprops.h \
	$(GNU_LIB)/timespec.h \
	$(CONFIG_H)

$(BLD)/md5.$(O) : \
	$(GNU_LIB)/md5.c \
	$(NT_INC)/stdalign.h \
	$(NT_INC)/stdint.h \
	$(CONFIG_H) \
	$(MD5_H)

$(BLD)/memrchr.$(O) : \
	$(GNU_LIB)/memrchr.c \
	$(CONFIG_H)

$(BLD)/sha1.$(O) : \
	$(GNU_LIB)/sha1.c \
	$(NT_INC)/stdalign.h \
	$(NT_INC)/stdint.h \
	$(CONFIG_H) \
	$(SHA1_H)

$(BLD)/sha256.$(O) : \
	$(GNU_LIB)/sha256.c \
	$(NT_INC)/stdalign.h \
	$(NT_INC)/stdint.h \
	$(CONFIG_H) \
	$(SHA256_H)

$(BLD)/sha512.$(O) : \
	$(GNU_LIB)/sha512.c \
	$(NT_INC)/stdalign.h \
	$(NT_INC)/stdint.h \
	$(CONFIG_H) \
	$(SHA512_H)

$(BLD)/stat-time.$(O) : \
	$(GNU_LIB)/stat-time.c \
	$(CONFIG_H) \
	$(STAT_TIME_H)

$(BLD)/timespec.$(O) : \
	$(GNU_LIB)/timespec.c \
	$(GNU_LIB)/timespec.h \
	$(CONFIG_H)

$(BLD)/u64.$(O) : \
	$(GNU_LIB)/u64.c \
	$(CONFIG_H) \
	$(U64_H)

$(BLD)/filemode.$(O) : \
	$(GNU_LIB)/filemode.c \
	$(CONFIG_H) \
	$(FILEMODE_H)

$(BLD)/sig2str.$(O) : \
	$(GNU_LIB)/sig2str.c \
	$(CONFIG_H) \
	$(SIG2STR_H)


# The following dependencies are for supporting parallel builds, where
# we must make sure $(BLD) exists before any compilation starts.
#
$(GNULIBOBJS): stamp_BLD

#
# Headers we would preprocess if we could.
#
getopt.h:	getopt_.h
	$(CP) $(ALL_DEPS) $@

#
# Maintenance
#
clean:
	- $(DEL) $(COMPILER_TEMP_FILES)
	- $(DEL) getopt.h
	- $(DEL_TREE) $(OBJDIR)
	- $(DEL) stamp_BLD

distclean:	cleanall
	- $(DEL) TAGS
	- $(DEL) Makefile

maintainer-clean: distclean
	- $(DEL) getopt_.h

cleanall:	clean
	- $(DEL_TREE) obj
	- $(DEL_TREE) obj-spd
	- $(DEL_TREE) oo
	- $(DEL_TREE) oo-spd

# A dummy target to force other targets to be evaluated.
doit:

getopt_.h: getopt.in.h $(ARG_NONNULL_H)
	$(MAKE) $(MFLAGS) getopt_.h-$(SHELLTYPE)

getopt_.h-CMD: doit
	@echo getopt.in.h or $(ARG_NONNULL_H) is newer than getopt_.h.
	@echo Run "$(MAKETYPE) getopt_h" in the lib/ subdirectory.
	@echo You will need GNU Sed to be installed.
	exit -1

getopt_.h-SH: doit
	@echo getopt.in.h or $(ARG_NONNULL_H) is newer than getopt_.h.
	@echo Run '"$(MAKETYPE) getopt_h"' in the lib/ subdirectory.
	@echo You will need GNU Sed to be installed.
	exit -1

# Generating getopt_.h from getopt.in.h.
#
# To avoid requiring all end users to install Sed, we have below
# a maintainer-only target that produces getopt_.h from getopt.in.h.
# For easier maintenance, all the strings that replace the @FOO@
# placeholder are defined as Make macros below.

HAVE_GETOPT_H = HAVE_GETOPT_H
INCLUDE_NEXT = include_next
PRAGMA_COLUMNS =
NEXT_GETOPT_H = <getopt.h>
ARG_NONNULL_H = ../build-aux/snippet/arg-nonnull.h

getopt_h:
	- $(DEL) getopt_.h-t getopt_.h
	sed -e "s!@GUARD_PREFIX@!GL!g" \
	    -e "s!@HAVE_GETOPT_H@!$(HAVE_GETOPT_H)!g" \
	    -e "s!@INCLUDE_NEXT@!$(INCLUDE_NEXT)!g" \
	    -e "s!@PRAGMA_SYSTEM_HEADER@!$(PRAGMA_SYSTEM_HEADER)!g" \
	    -e "s!@PRAGMA_COLUMNS@!$(PRAGMA_COLUMNS)!g" \
	    -e "s!@NEXT_GETOPT_H@!$(NEXT_GETOPT_H)!g" \
	    -e "/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)" \
	    -e "/^\# include_next/i # ifndef __GNUC__" \
	    -e "/^\# include_next/i # include <next_getopt.h>" \
	    -e "/^\# include_next/i # else" \
	    -e "/^\# include_next/a # endif" \
	      < getopt.in.h > getopt_.h-t
	$(CP) getopt_.h-t getopt_.h
	- $(DEL) getopt_.h-t

execinfo.h: execinfo.in.h
	$(CP) execinfo.in.h $@
